// pages/redPacket/redPacket.js
const redPacketService = require('../../../../api/redPacketService.js')
const auth = require('../../../../utils/auth.js')
const utils = require('../../../../utils/utils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance: '0.00',
    termValidity: '2018.11.11  23:59:59',
    balanceList: [],
    page: 1,
    pageSize: 20,
    pageCount: 1,
    show: {
      middle: false,
      top: false,
      bottom: false,
      right: false,
      right2: false
    },
    step: 'first'
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
    var _userInfo = auth.getUser();
    if (_userInfo && _userInfo.member) {
      this.setData({
        mobile: _userInfo.member.mobile
        // mobile: '16311252631' //测试手机号
      })
      this.handelRedPacketBalance()
    }
    let now = new Date()
    let time = (utils.formatTime(now)).replace(/\//g, '-')
    let tempTimeArr = time.split(" ")
    let tempTermValidity = tempTimeArr[0] + ' 23:59:59'
    this.setData({
      termValidity: tempTermValidity
    })
    try {
      var value = wx.getStorageSync('wj_redCount')
      if (value) {
        let tempTime = tempTermValidity.replace(/-/g, '/')
        let nowTime = new Date().getTime()
        let time = new Date(tempTime).getTime()
        if (nowTime > time) {
          this.toggleMiddlePopup()
        }
        console.log(tempTime, time)
      } else {
        this.toggleMiddlePopup()
      }
      wx.setStorageSync('wj_redCount', tempTermValidity)
    } catch (error) {

    }
    this.handelQueryList(this.data.page);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 调用接口查询红包余额
   */
  handelRedPacketBalance() {
    redPacketService.getBalance().then(res => {
      console.log(res)
      this.setData({
        balance: res
      })
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 调用接口获取储值明细
   */
  handelQueryList: function (page) {
    let searchCount = true
    redPacketService.queryRedPacketHst(page, this.data.pageSize, searchCount, this.data.mobile).then(res => {
      // console.log(res)
      if (res && res.records && res.records.length >= 0) {
        let tempbalanceList = this.data.balanceList;
        res.records.forEach(item => {
          let tempState = true; // true 表示正数，false表示负数
          let tempAction = '';
          if (item.occurTotal < 0) {
            tempState = false
            tempAction = '消费'
          } else {
            tempState = true
            tempAction = '系统发放'
          }
          tempbalanceList.push({
            ...item,
            state: tempState,
            action: tempAction
          })
        })
        let pageCount = parseInt(res.recordCount / res.pageSize) + 1;
        this.setData({
          page: res.page,
          pageCount: pageCount,
          balanceList: tempbalanceList
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 打开弹窗
   */
  toggleMiddlePopup() {
    this.toggle('middle');
    this.setData({
      step:'first'
    })
  },
  toggle(type) {
    this.setData({
      [`show.${type}`]: !this.data.show[type]
    });
  },
  toggleNextStep: function () {
    this.setData({
      step: 'second'
    })
  }
})